<?php 
	$header = array(
		'header' => $header,
		'sub_header' => $post_title
		);
?>
<?php getView('modules/header',$header); ?>

<?php getView('modules/admin-menu'); ?>

<div id="content-wrapper" class="container-fluid">
	<div class="row">
		<?php getView('modules/admin-sidebar', array('menu_active',$menu_active)); ?>
		<div id="admin-content">
			<div class="col-xs-12">
				<!-- Show Alert -->
				<?php 
					if(isset($alert))
						getView('modules/admin-alert', $alert);
				?> 
				<div class="below-alert"></div>
				<?php 
					$attr = array(
						'id' => 'form-post'
						);
					echo form_open('admin/post_edit/postUpdate', $attr);
				?>
					<div class="row">
						<div class="col-xs-12">
							<h2 class="mgb15 text-left">Edit post <a href="post_new" class="btn btn-primary btn-xs">Add New</a></h2>
						</div>
						<div class="col-xs-12 col-smm-6 col-sm-8">
							<input type="text" class="form-control" value="<?php echo $post_title; ?>" placeholder="Enter title here" name="post_title">
							<div class="mgt10">
								<?php getView('modules/tinymce', array('getContent' => $post_content)); ?>
							</div>
						</div>
						<div class="col-xs-12 col-smm-6 col-sm-4">
							<div class="newpost-item">
								<h5 class="title">Publish</h5>
								<div class="content">
									<div class="input-group datetime-picker">
										<span class="input-group-addon pointer">
											<span class="glyphicon glyphicon-calendar"></span>
										</span>
										<input type="text" name="post_date" class="form-control">
									</div>
									<div class="mgt10">
										<select name="post_status" class="select-tize form-control" placeholder="Post status" readonly>
											<option value="publish">Publish</option>
											<option value="trash">Trash</option>
										</select>
									</div>
									<div class="mgt10 clearfix">
										<input type="hidden" name="post_id" value="<?php echo $ID; ?>">
										<input type="submit" id="publish-post" class="btn btn-primary pull-right" value="Save">
									</div>
								</div>
							</div>
							<div class="newpost-item categories-list">
								<h5 class="title">Categories</h5>
								<div class="content">
									<?php echo $terms_tree; ?>
								</div>
							</div>
							<div class="newpost-item featured-image">
								<h5 class="title">Featured Image</h5>
								<div class="content">
									<div class="thumbnail <?php if(empty($post_featured)) echo 'hidden'; ?>">
										<?php if(!empty($post_featured)): ?>
										<img src="<?php echo $post_featured; ?>" alt="" class="img-responsive">
										<?php endif; ?>
									</div>
									<input id="input-featured-image" name="post_featured" type="hidden" value="<?php echo $post_featured; ?>">
									<a class="<?php if(!empty($post_featured)) echo 'hidden'; ?>" href="<?php site_url(); ?>/filemanager/dialog.php?type=1&field_id=input-featured-image" id="set-featured-image">Set featured-image</a>
									<a href="#" id="remove-featured-image" class="<?php if(empty($post_featured)) echo 'hidden'; ?>">Remove featured-image</a>
								</div>
							</div>
						</div>
					</div>
				</form>
				<?php getView('modules/footer'); ?>
			</div>
		</div>
	</div>
</div>
<script>
	
	var $select = $('.select-tize').selectize();
	var control = $select[0].selectize;
	control.setValue('<?php echo $post_status; ?>');

	$('input[name="post_date"]').val('');
	$('.datetime-picker').datetimepicker({
		format: "YYYY-MM-DD HH:mm:ss",
		locale: "vi",
		defaultDate: moment(),
	});

	var terms = <?php echo json_encode($post_terms); ?>;
	for(term in terms){
		cate = terms[term]['term_id'];
		$('input[name="terms[]"]').each(function(index, el) {
			if($(this).val() == cate)
				$(this).prop('checked', true);
		});
	}

	function responsive_filemanager_callback(field_id){ 
	    var img = $('#input-featured-image').val();
	    $('.featured-image .thumbnail').html('<img class="img-responsive" src="'+img+'">');
	    $('.featured-image .thumbnail').imagesLoaded(function(){
	    	$('.featured-image .thumbnail').removeClass('hidden');
	    });
	    $('#set-featured-image').addClass('hidden');
	    $('#remove-featured-image').removeClass('hidden');
	} 
	$('#remove-featured-image').click(function(event) {
		/* Act on the event */
		event.preventDefault();
		$('.featured-image .thumbnail').empty().addClass('hidden');
		$('#input-featured-image').val('');
		$('#set-featured-image').removeClass('hidden');
		$('#remove-featured-image').addClass('hidden');
	});

	$('#set-featured-image').fancybox({	
		'width'		: 992,
		'height'	: 600,
		'type'		: 'iframe',
        'autoScale'    	: false
    });

    $('#form-post').validate({
		rules:{
			post_title: 'required'
		}
	});

    $('#form-post').submit(function(){
    	if($('#form-post').valid()){
    		var flag = false;
    		if(flag) return false; flag = true;

    		tinymce.triggerSave();
    		$.post($('#form-post').attr('action'), $('#form-post').serializeArray(), function(json) {
    			if(json.status == 0){
    				adminAlert(json.message, 3000, 'error');
    			}else{
    				window.location.href = json.message;
    			}
    		}, 'json');
    	}
    	return false;
    });
	/* $('#publish-post').click(function(event) {
		event.preventDefault();
		var flag = false;
		if($('#form-post').valid()){
			var post_title = $('input[name="post_title"]').val(),
				post_content = editor.getContent(),
				post_date = $('input[name="post_date"]').val(),
				post_cate = $('input[name=terms]:checked').map(function() {
		         	return $(this).val();
		        }).toArray(),
				post_status = $('select[name="post_status"]').val();
				post_featured = $('input[name="post_featured"]').val();

			var data = {
				post_title : post_title,
				post_content : post_content,
				post_date : post_date,
				post_cate : post_cate,
				post_status : post_status,
				post_featured : post_featured,
				send: randNumber()
			};
			if(flag) return false;
			flag = true;

			$.ajax({
				url: 'post_edit/updatePost',
				type: 'POST',
				data: data,
			})
			.done(function(data) {
				if(data!='0'){
					window.location.href = 'post_edit/'+data;
				}else{
					adminAlert('Error post', 3000, 'error');
				}
			})
			.fail(function(data) {
				adminAlert('Error post', 3000, 'error');
			})
			.always(function() {
				flag = false;
			});
			
		}
	}); */

</script>
</body>
</html>